package com.applink.sell_proof.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.applink.sell_proof.EMarketing;
import com.applink.sell_proof.R;
import com.applink.sell_proof.pojo.GenericObject;
import com.applink.sell_proof.utilXMLParser.AndroidUIParser;
import com.applink.sell_proof.utilXMLParser.ColumnSetting;

public class RewardClaimDetailConfigAdapter extends BaseAdapter {

	private LayoutInflater mInflater;
	private Context mContext;
	private ArrayList<GenericObject> mGenericObjectList = new ArrayList<GenericObject> ();
	private ArrayList<ColumnSetting> mColumnSettingList = new ArrayList<ColumnSetting>();

	public RewardClaimDetailConfigAdapter(Context mContext, ArrayList<GenericObject> objects,ArrayList<ColumnSetting> columnSettings){
		
		mInflater = LayoutInflater.from(EMarketing.getInstance().getApplicationContext());
		this.mContext = mContext;
		this.mGenericObjectList= objects;
		this.mColumnSettingList = columnSettings;	
	}

	@Override
	public int getCount() {
		return this.mGenericObjectList.size();	
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		try {
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.reward_claim_detail_item, null);	
			}	
	       	
	        final GenericObject object = this.mGenericObjectList.get(position);
	        if (object != null) {
	        	  	 
	        	 // setting list_item views
	        		for(int i=0; i< mColumnSettingList.size();i++){
		        		ColumnSetting columnSetting = new ColumnSetting();
		        		columnSetting  = mColumnSettingList.get(i);
            		
		        		// find id of Textview based on columnSetting id by reflection
            			int idControl = AndroidUIParser.getIdFromAndroidUIWith_SureID(this.mContext,columnSetting.getMappingName());	        			    
            			if(idControl == -1) { // this xml file doesn't have that ID control
            			    return null;
            		    }

         			    TextView tvId	 = (TextView) convertView.findViewById(idControl);
         			    String content = object.getContentFromObjectMappingName(columnSetting.getMappingName());
         			    tvId.setText(content);        			  
            	  }
	        }

	 } catch (Exception e) { 
        	 System.out.println(e.toString());
        	 convertView = null;
      }
         
	return convertView;
	}

}
